package cn.itsource.pet.controller;

import cn.itsource.basic.util.AxajResult;
import cn.itsource.basic.util.PageInfo;
import cn.itsource.basic.util.SystemLoginInfo;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.LoginInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IEmployeeService employeeService;
    /**
     * 保存寻主信息
     * @param searchMasterMsg
     * @return
     */
    @PostMapping("/publish")
    public AxajResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
            //在请求头中获取token值
            String token = request.getHeader("token");
            //根据token获取登录用户
            LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
            searchMasterMsgService.publish(searchMasterMsg, loginInfo);
            return new AxajResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxajResult(false,e.getMessage());
        }
    }
    @PatchMapping
    @ApiOperation(value = "查询所有未处理的寻主信息",notes = "很好用的接口")
    public PageInfo<SearchMasterMsg> findAll(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request){
        LoginInfo loginInfo = SystemLoginInfo.get(request);
        //通过登录用户获取employee对象
        Employee employee=employeeService.findByLoginInfoId(loginInfo.getId());
        searchMasterMsgQuery.setShopId(employee.getShop().getId());
        //分页查询
        return  searchMasterMsgService.findAllByQuery(searchMasterMsgQuery);
    }
}
